
/**
 * Project Name:cats-core-impl
 * File Name:ResourceServiceImpl.java
 * Package Name:com.catsic.core.resource.service.impl
 * Date:2014年3月24日下午3:38:48
 * Copyright (c) 2014, oa.catsic.com All Rights Reserved.
 */
 
package com.catsic.core.resource.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.catsic.base.BaseServiceImpl;
import com.catsic.base.exception.CatsicException;
import com.catsic.base.status.Status;
import com.catsic.base.tree.TreeInfo;
import com.catsic.core.autharea.service.AuthAreaService;
import com.catsic.core.constants.Constants;
import com.catsic.core.convert.Converts;
import com.catsic.core.entity.AuthArea;
import com.catsic.core.entity.Menu;
import com.catsic.core.entity.Resource;
import com.catsic.core.entity.Role;
import com.catsic.core.exception.CatsicExceptionCode;
import com.catsic.core.form.ResourceForm;
import com.catsic.core.menu.service.MenuService;
import com.catsic.core.organ.service.OrganService;
import com.catsic.core.resource.dao.ResourceDao;
import com.catsic.core.resource.service.ResourceService;
import com.catsic.core.role.service.RoleService;
import com.catsic.core.user.service.UserService;
import com.catsic.pub.drawer.service.DrawerService;
import com.catsic.pub.entity.Drawer;

/**
 * ClassName: ResourceServiceImpl
 * Description: 资源Service
 * date: 2014年3月24日 下午3:38:48
 * author: yangyd
 */
@Service("resourceService")
public class ResourceServiceImpl extends BaseServiceImpl<Resource, String> implements ResourceService{

	@Autowired
	private ResourceDao resourceDao;
	
	@Autowired
	private OrganService organService;
	
	@Autowired
	private MenuService menuService;
	
	@Autowired
	private UserService userService;
	
	@Autowired
	private DrawerService drawerService;
	
	@Autowired
	private AuthAreaService authAreaService;
	
	@Autowired
	private RoleService roleService;
	
	/**
	 * 添加资源
	 * @see com.catsic.core.base.BaseServiceImpl#add(java.lang.Object)
	 */
	/**
	 * @see com.catsic.core.resource.service.ResourceService#add(com.catsic.core.form.ResourceForm)
	 **/
	@Override
	public ResourceForm add(ResourceForm form){
		Menu menu = menuService.findByCode(form.getMenuCode());
		Drawer drawer = drawerService.findByCode(form.getDrawerCode());
		AuthArea authArea = authAreaService.findByCode(form.getAuthAreaCode());
		Resource resource = new Resource();
		resource.setAssembly(form.getAssembly());
		resource.setJsFunction(form.getJsFunction());
		resource.setRevent(form.getRevent());
		resource.setName(form.getName());
		resource.setIcon(form.getIcon());
		resource.setCode(code(menu.getCode()));
		resource.setPattern(form.getPattern());
		resource.setViews(form.getViews());
		resource.setMenu(menu);
		resource.setDrawer(drawer);
		resource.setAuthArea(authArea);
		resource = super.add(resource);
		return Converts.convert(resource);
	}
	
	/**
	 * 生成编码规则
	 * @param pid
	 * @return
	 */
	private String code(String menuCode){
		/**
		 * 查询父节点下的所有子节点
		 */
		List<Resource> childs = resourceDao.findByCodeIsNotNullAndMenuCodeOrderByCodeDesc(menuCode);
		if(CollectionUtils.isEmpty(childs)){
			return menuCode + String.format("%0" + Constants.编码位数 + "d",0);
		}
		String code = childs.get(0).getCode();
		int length = code.length();
		return String.format("%0" + length + "d", Long.valueOf(code)+ 1);
	}
	
	/**
	 * @see com.catsic.core.resource.service.ResourceService#update(com.catsic.core.form.ResourceForm)
	 */
	public ResourceForm update(ResourceForm form){
		Resource resource = findById(form.getId());
		resource.setPattern(form.getPattern());
		resource.setViews(form.getViews());
		resource.setIcon(form.getIcon());
		resource.setName(form.getName());
		resource.setAssembly(form.getAssembly());
		resource.setJsFunction(form.getJsFunction());
		resource.setRevent(form.getRevent());
		resource.setAuthArea(authAreaService.findByCode(form.getAuthAreaCode()));
		resource.setDrawer(drawerService.findByCode(form.getDrawerCode()));
		resource.setMenu(menuService.findByCode(form.getMenuCode()));
		resource = super.add(resource);
		return Converts.convert(resource);
	}
	
	/**
	 * @see com.catsic.core.resource.service.ResourceService#findById(java.lang.Long)
	 */
	@Override
	public Resource findById(String id){
		Resource resource = getById(id);
		if(null == resource){
			throw new CatsicException(CatsicExceptionCode.实体不存在,Constants.RESOURCE);
		}
		return resource;
	}
	
	
	/**
	 * @see com.catsic.core.resource.service.ResourceService#cancel(java.lang.String)
	 **/
	@Override
	public void cancel(String id){
		Resource resource = findById(id);
		resource.setStatus(Status.无效);
		super.add(resource);
	}
	
	/**
	 * @see com.catsic.core.resource.service.ResourceService#delete(java.lang.String)
	 */
	@Override
	public void delete(String id){
		Resource resource = findById(id);
		List<Role> list = roleService.findRoleByResourceId(resource.getId());
		if(CollectionUtils.isNotEmpty(list)){
			throw new CatsicException(CatsicExceptionCode.资源管理_行为已绑定角色_无法删除);
		}
		delete(resource);
	}
	
	/**
	 * @see com.catsic.core.resource.service.ResourceService#active(java.lang.String)
	 */
	@Override
	public void active(String id){
		Resource resource = findById(id);
		resource.setStatus(Status.有效);
		super.add(resource);
	}
	
	/**
	 * @see com.catsic.core.resource.service.ResourceService#findByName(java.lang.String)
	 */
	@Override
	public List<Resource> findByName(String name) {
		List<Resource> resources = resourceDao.findByName(name);
		if(CollectionUtils.isEmpty(resources)){
			throw new CatsicException(CatsicExceptionCode.角色管理_权限不能为空,Constants.RESOURCE);
		}
		return resources;
	}
	
	/**
	 * @see com.catsic.core.resource.service.ResourceService#findByLikeCode(java.lang.String)
	 */
	public List<Resource> findByLikeCode(String code){
		return resourceDao.findByLikeCode(code);
	}
	
	/**
	 * @see com.catsic.core.resource.service.ResourceService#findByCode(java.lang.String)
	 */
	@Override
	public Resource findByCode(String code) {
		Resource resource = resourceDao.findByCode(code);
		if(resource == null){
			throw new CatsicException(CatsicExceptionCode.实体不存在,Constants.RESOURCE);
		}
		return resource;
	}
	
	/**
	 * @see com.catsic.core.resource.service.ResourceService#findByUserIdOrderByCodeDesc(java.lang.String)
	 */
	public List<Resource> findByUserIdOrderByCodeAsc(String id){
		return resourceDao.findByUserIdOrderByCodeAsc(id);
	}
	
	/** 
	 * @see com.catsic.core.resource.service.ResourceService#findByMenuCode(java.lang.String)
	 */
	public List<Resource> findByMenuCode(String menuCode){
		return resourceDao.findByMenuCode(menuCode);
	}
	
	/**
	 * @see com.catsic.core.resource.service.ResourceService#getTreeInfo(java.lang.String)
	 */
	public List<TreeInfo> getTreeInfo(String id){
		List<Resource> list = new ArrayList<Resource>();
		/**
		 * StringUtils.isBlank(id)条件为点击主页面权限管理功能时触发
		 */
		if(StringUtils.equals(id, Constants.ENTITY_ID) || StringUtils.isBlank(id)){
			list = resourceDao.findAll();
		}else{
			list = resourceDao.findByUserIdOrderByCodeAsc(id);
		}
		List<TreeInfo> listTreeInfo = new ArrayList<TreeInfo>();
		for(Resource resource : list){
			TreeInfo treeInfo = Converts.convertTree(resource);
			if(resource.getCode().length() == 12 || resource.getCode().length() == 7){
				treeInfo.setParent(false);
			}
			if(StringUtils.equals(resource.getId(), Constants.ENTITY_ID)){
				treeInfo.setOpen(true);
			}
			listTreeInfo.add(treeInfo);
		}
		return listTreeInfo;
	}
	
	/**
	 * @see com.catsic.core.resource.service.ResourceService#query(java.lang.String)
	 */
	public List<ResourceForm> query(String menuCode){
		List<Resource> list = findByMenuCode(menuCode);
		List<ResourceForm> listForm = new ArrayList<ResourceForm>();
		for(Resource menu : list){
			ResourceForm form = Converts.convert(menu);
			listForm.add(form);
		}
		return listForm;
	}
	
	/**
	 * @see com.catsic.core.resource.service.ResourceService#findResourcesByUserId(java.lang.String,java.lang.String,java.lang.String,java.lang.String)
	 */
	public List<ResourceForm> findResourcesByUserId(String userId,String menuCode,String drawerCode,String authAreaCode){
		List<Menu> listMenu = menuService.findByUserIdOrderByCodeAsc(userId);
		boolean bool = false;
		for(Menu menu : listMenu){
			if(StringUtils.equals(menu.getCode(), menuCode)){
				bool = true;
			}
		}
		if(!bool){
			throw new CatsicException(CatsicExceptionCode.无权限_拒绝访问);
		}
		List<Resource> list = resourceDao.findResourcesByUserIdOrderByCodeAsc(userId,menuCode,drawerCode,authAreaCode);
		List<ResourceForm> listForm = new ArrayList<ResourceForm>();
		for(Resource menu : list){
			ResourceForm form = Converts.convert(menu);
			listForm.add(form);
		}
		return listForm;
	}
}
